-- 条件检测中心

local ngx = ngx

local mis_common = nil
local mis_log = nil
local mis_config = nil
local mis_data = nil

--------------------------------------------------------------
local mis_condition = {
	s_name = string.match(debug.getinfo(1).short_src, '.*/(.*).lua');	-- 模块名称
}
local this = mis_condition

--------------------------------------------------------------
-- 说明：模块包含
-- 参数：模块表
this.modules_include = function(t_moddules)
	mis_common = t_moddules.mis_common
	mis_config = t_moddules.mis_config
	mis_log = t_moddules.mis_log
	mis_data = t_moddules.mis_data
end

-- ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
-- 说明：检测函数
-- 参数：_conf			配置
--		_data			实际数据(apiid, ver, lang, country必定存在，除此之外其他字段需要判空)
-- 返回：b
-- 渠道
this.apiid = function(_conf, _data)
	return _conf[_data.apiid]
end

-- 版本号
this.ver = function(_conf, _data)
	local n_ver = mis_common.version_to_number(_data.ver)
	return _conf.n_l <= n_ver and n_ver <= _conf.n_r
end

-- 语言
this.lang = function(_conf, _data)
	return _conf[_data.lang]
end

-- 国家
this.country = function(_conf, _data)
	return _conf[_data.country]
end

-- 流失天数
this.lostday = function(_conf, _data)
	if not _data.last_login_60days or _data.last_login_60days == ngx.null then
		return false
	end
	local n_lostday = mis_common.day_offset(_data.last_login_60days, ngx.today()) - 1
	return _conf.n_l <= n_lostday and n_lostday <= _conf.n_r
end

-- 注册天数
this.registerday = function(_conf, _data)
	-- 当没有create_at的时候，代表今天刚建号
	if not _data.create_at or _data.create_at == ngx.null then
		return _conf.n_l == 1
	end
	local n_registerday = mis_common.day_offset(_data.create_at, ngx.today()) + 1
	return _conf.n_l <= n_registerday and n_registerday <= _conf.n_r
end

-- 支付金额
this.pay = function(_conf, _data)
	if not _data.pay_money or _data.pay_money == ngx.null then
		return _conf.n_l == 0
	end
	return _conf.n_l <= _data.pay_money and _data.pay_money <= _conf.n_r
end
-- ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑

--------------------------------------------------------------
-- 说明：标签条件检查
-- 参数：t_labelConf, t_label
-- 返回：n 					标签建议状态
this.check_label = function(t_labelConf, t_label)
	local t_params = ngx.ctx.t_params
	local n_now = ngx.time()
	local t_labelStatus = mis_config.t_labelStatus

	-- 下架
	if not t_labelConf then
		return t_labelStatus.n_DELETE
	end
	-- 死亡
	if t_label and t_label.status == mis_config.t_labelStatus.n_DEAD then
		return t_labelStatus.n_DEAD
	end
	-- 最低版本号
	if t_labelConf.n_verMin then
		if not t_params.n_ver then t_params.n_ver = mis_common.version_to_number(t_params.ver or '0') end
		if t_params.n_ver < t_labelConf.n_verMin then
			return t_labelStatus.n_DISABLE
		end
	end
	-- 新手标签
	if t_labelConf.n_type == mis_config.t_labelType.n_NEW then
		if not t_label or t_label.status == t_labelStatus.n_DISABLE then
			if not t_params.createTime then
				return t_labelStatus.n_DISABLE
			elseif t_params.createTime + 604800 < n_now then	-- 604800: 7 days
				return t_labelStatus.n_DEAD
			end	
		end
	end
	-- 活动时长
	if t_label and t_labelConf.n_duration and t_label.activateTime and t_label.activateTime + t_labelConf.n_duration < n_now then
		return t_labelStatus.n_DISABLE
	end
	-- ABTest
	if not t_label and t_labelConf.t_ABTest then
		for _, s_key in ipairs(t_labelConf.t_ABTest) do
			local n_bucketId, n_res = mis_data.t_ABTestSDK.getABTestExp(s_key, t_params.uin)
			if not n_bucketId or n_res ~= 1 then
				return t_labelStatus.n_DEAD
			end
		end
	end
	return t_labelStatus.n_ENABLE
end

--------------------------------------------------------------
-- 说明：随机机制条件检查
-- 参数：randomConf, t_misData
-- 返回：b
this.check_random = function(t_randomConf, t_misData)
	-- 每日固定时间点刷新
	if t_randomConf.n_daily and mis_common.is_another_time(t_misData.loginTime or 0, ngx.time(), t_randomConf.n_daily) then
		return false
	end
	return true
end

--------------------------------------------------------------
-- 说明：任务条件检查
-- 参数：t_misConf, t_misData, n_labelId, n_taskId
-- 返回：b
this.check_task = function(t_misConf, t_misData, n_labelId, n_taskId)
	local n_now = ngx.time()
	local t_labelConf = t_misConf.t_conf.t_label[n_labelId]
	local t_taskConf = t_labelConf.t_task[n_taskId]
	local t_task = t_misData and t_misData.label[n_labelId].task[n_taskId]
	-- 下架
	if not t_taskConf then
		return false
	end
	-- 开始时间
	if t_taskConf.n_startTime and n_now < t_taskConf.n_startTime then
		return false
	end
	-- 结束时间
	if t_taskConf.n_endTime and t_taskConf.n_endTime <= n_now then
		return false
	end
	if t_misData then
		-- 是随机label，有每周刷新或者每日固定时间点刷新，或者都没有但是还未完成
		if t_labelConf.n_type == mis_config.t_labelType.n_RANDOM then
			local t_randomConf = t_labelConf.t_random
			-- 转盘每日固定时间点刷新（大刷新）
			-- 如果未完成，所有任务都支持大刷新
			-- 如果已完成，每日、每周刷新任务支持大刷新，一次性任务不支持大刷新
			if (not t_task.status or t_taskConf.n_daily or t_taskConf.b_weekly) and t_randomConf.n_daily and mis_common.is_another_time(t_misData.loginTime or 0, n_now, t_randomConf.n_daily) then
				return false
			end
			-- 随机定时刷新（小刷新）
			-- 如果未完成，所有任务都支持小刷新
			-- 如果已完成，所有任务不支持小刷新
			if not t_task.status and t_randomConf.n_refreshTime and t_task.activateTime + t_randomConf.n_refreshTime <= n_now then
				return false
			end
		end
		-- 每周刷新
		if t_taskConf.b_weekly and mis_common.is_another_week(t_misData.loginTime or 0, n_now) then
			return false
		end
		-- 每日固定时间点刷新
		if t_taskConf.n_daily and mis_common.is_another_time(t_misData.loginTime or 0, n_now, t_taskConf.n_daily) then
			return false
		end
	end
	return true
end

return this